//
//  ColorPickerCell.swift
//  PorscheRegistration
//
//  Created by Jinzhu Liu on 2023/11/22.
//

import UIKit

class ColorPickerCell: NiblessTableViewCell, ViewModelUpdatableCell {
    var viewModel: AnyObject?
    
    var colorView: UIView
    var nameLabel: UILabel
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        self.colorView = UIView()
        self.nameLabel = UILabel()
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    func setupUI() {
        colorView.layer.cornerRadius = 15.0
        colorView.clipsToBounds = true
        
        self.contentView.addSubview(colorView)
        self.contentView.addSubview(nameLabel)
        
        colorView.translatesAutoresizingMaskIntoConstraints = false
        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            colorView.heightAnchor.constraint(equalToConstant: 30.0),
            colorView.widthAnchor.constraint(equalTo: colorView.heightAnchor),
            colorView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 16.0),
            colorView.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor),
            
            nameLabel.centerYAnchor.constraint(equalTo: colorView.centerYAnchor),
            nameLabel.heightAnchor.constraint(equalToConstant: 30.0),
            nameLabel.leadingAnchor.constraint(equalTo: self.colorView.trailingAnchor, constant: 10.0),
            nameLabel.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -16.0)
            
        ])
    }
    
    public func update(with viewModel: AnyObject?) {
        if let viewModel = viewModel {
            self.viewModel = viewModel
            if let cellViewModel = viewModel as? ColorPickerCellViewModelType {
                self.colorView.backgroundColor = cellViewModel.output.color
                self.nameLabel.text = cellViewModel.output.name
            }
        }
    }
}


